<?php
/**
 * Created by PhpStorm.
 * User: lhl
 * Date: 2020/11/3
 * Time: 16:23
 * Features: 自学习配置
 */

namespace app\adminapiv3\model;


use app\adminapiv3\model\learn_data\HostLearnDataModel;
use think\facade\Cache;

class HostLearnConfigModel extends BaseModel
{
    const LEARN_ID_PREV_CACHE_KEY_PREFIX = 'learn_id_prev_cache:';

    /**
     * 根据主机id,设置当前最新批次的学习id数组缓存
     * @param int   $hostId       主机id
     * @param int   $batchNum     批次号,需要是开始学习前的最新批次号
     * @param bool  $isNewModel   是否为全新学习模式,全新模式下,缓存中id数据设置为空数组且批次号固定为0
     */
    public function setLearnIdCache($hostId,$batchNum,$isNewModel){
        $key = self::LEARN_ID_PREV_CACHE_KEY_PREFIX . $hostId;
        $learnModel = new HostLearnDataModel();
        $ids = $isNewModel ? [] :$learnModel->getLearnIdByHostIdAndBatchNo($hostId,$batchNum);
        $batchNum = $isNewModel ? 0 : $batchNum;
        //设置缓存
        $data = [
            'host_id' => $hostId,
            'learn_number' => $batchNum,
            'ids' => $ids
        ];
        return Cache::set($key,$data);
    }

    /**
     * 根据主机id,获取上个批次的学习id数组缓存
     * @param int $hostId       主机id
     */
    public function getLearnIdCache($hostId){
        $key = self::LEARN_ID_PREV_CACHE_KEY_PREFIX . $hostId;
        return Cache::get($key);
    }

    /**
     * 批量设置主机当前最新批次的学习id数组缓存
     * [
     *      'host_id' =>    主机id
     *      'oldBatchNo' => 原批次号
     * ]
    */
    public function batchSetLearnIdCache(array $batchs){
        //循环设置缓存
        $result = [];
        foreach ($batchs as $item){
            $res = $this->setLearnIdCache($item['host_id'],$item['oldBatchNo'],$item['isNewModel']);
            $result[] = [
                'host_id' => $item['host_id'],
                'result' => $res
            ];
        }
       return $result;
    }


    /**
     * 根据host_id查询单条配置信息
     * @param int $hostId 主机id
    */
    public function getConfigByHostId($hostId,$field = 'c.*,h.name'){
        return $this->alias('c')->leftJoin('host h','c.host_id=h.id')
            ->where('c.host_id',$hostId)
            ->field($field)->findOrEmpty()->toArray();
    }

    /**
     * 根据主机id查询配置信息
     * @param string $hostIds 主机id,逗号隔开字符串
     * @param string $field   查询字段
    */
    public function getConfigListByHostId($hostIds,$field = 'c.*,h.name'){
        return $this->alias('c')->leftJoin('host h','c.host_id=h.id')
            ->where('c.host_id','in',$hostIds)
            ->field($field)->select()->toArray();
    }


    /**
     *  根据主机id字符串查询是否接管了防火墙
     *  @param string $hostIds 主机id,逗号隔开字符串
     */
    public function isControlledFirewallList($hostIds){
        return $this->alias('c')->leftJoin('host h','c.host_id=h.id')
            ->where('c.host_id','in',$hostIds)
            ->field('c.host_id,c.controlled_firewall,h.name')->select();
    }

    /**
     *  根据主机id查询该主机的危险端口
     *  @param int $hostId 主机id
     */
    public function getDangerPortByHostId($hostId){
        return $this->where('host_id',$hostId)->value('danger_port');
    }

    /**
     *  根据主机id查询该主机的当前学习批次号
     *  @param int $hostId 主机id
     */
    public function getLearnNunByHostId($hostId){
        return $this->where('host_id',$hostId)->value('learn_number');
    }


}